package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.util.QiNiuUploadUtil;
import cn.wolfcode.trip.admin.util.UploadUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("images")
public class ImageController {
    /**
     * 图片的上传
     * @return
     */
    @RequestMapping("/upload")
    public Object upload(MultipartFile upload){ // 插件默认参数名必须是upload
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            // 将用户的头像上传至图片服务器
            String url = QiNiuUploadUtil.upload(upload, UploadUtil.BASE_PATH + "upload");
            // 返回图片的url,用户回显
            map.put("url",url);
            map.put("uploaded",1);
        } catch (Exception e) {
            map.put("uploaded",0);
            Map<String,Object> error = new HashMap<String, Object>();
            error.put("message",e.getMessage());
            map.put("error",error);
        }
        return map;
    }
}

